//Author: Max Charnock
//Date: 10/02/2015
//Purpose: Manages HUD entities and sends them to the renderer to Draw
//Singleton Classs

#include "FPSCounter.h"
#include "Button.h"
#include "../../nclgl/ControllerHeader.h"
#include "XBOXController.h"

struct HighScorePoints
{
public:

	string Name;

	int Score;
};


class HUDManager
{
protected:

	HUDManager();
	~HUDManager(); 

	/*pointer to HUDmanager instance*/
	static HUDManager* pInst;

	FpsCounter Render_Counter;
	FpsCounter Physics_Counter;

	bool Debug_Mode;
	bool Paused;
	bool Exit;
	bool Running;
	bool Reset_HUD;
	bool Fullscreen;

	float GameTime;
	int Player_Health;

	vector<Button*> PauseButtons;
	vector<Button*> StartButtons;

	vector<Button*>::iterator Selected_Button;

	vector<Button*>* ActiveList;

	HighScorePoints High_Score[10];

	SceneNode Logo;
	SceneNode Health_Bar;

	XBOXController* playerXbox_controller;

public:

	//Gets/Created HUDManager instance
	static HUDManager* GetInstance();

	//Releases HUDManager Instance
	void Release();

	void Intialise();

	void Update(float msec);

	void LoadHighScore(char* filename);
	void WriteHighScore(char* filename);
	void AddScore(string name ,int score);

	void MoveMenuUp();
	void MoveMenuDown();


	void UpdateRenderFPS(float msec)	{Render_Counter.Update(msec);}
	int GetRenderFPS()					{return Render_Counter.GetFPS();}

	void UpdatePhysicsFPS(float msec)	{Physics_Counter.Update(msec);}
	int GetPhysicsFPS()					{return Physics_Counter.GetFPS();}

	void ToggleDebugMode() { Debug_Mode = !Debug_Mode;}
	bool GetDebugMode() {return Debug_Mode;}

	void TogglePaused() { Paused = !Paused;}
	bool GetPaused() {return Paused;}

	void ToggleExit() { Exit = !Exit;}
	bool GetExit() {return Exit;}

	void ToggleReset_HUD() { Reset_HUD = !Reset_HUD;}
	bool GetReset_HUD() {return Reset_HUD;}

	float GetGameTime(){return GameTime;}

	void SetPlayerXboxController(XBOXController* controller){ playerXbox_controller = controller; }

	void Reset();

	void SetPlayerHealth(int player){Player_Health = player;}

	vector<SceneNode*> GetDrawList();

	vector<string> GetHighScoreText();

	void ToggleRunning() 
	{
		Running = !Running;
		if(Running)
		{
			(*Selected_Button)->ToggleSelected();
			ActiveList = &PauseButtons;
			Selected_Button = ActiveList->end()-1;
			(*Selected_Button)->ToggleSelected();
		}
		else
		{
			(*Selected_Button)->ToggleSelected();
			ActiveList = &StartButtons;
			Selected_Button = ActiveList->end()-1;
			(*Selected_Button)->ToggleSelected();
		}
	}
	bool GetRunning() {return Running;}

	void ToggleFullscreen()
	{
		Fullscreen = !Fullscreen;

		if(Fullscreen)
		{
			SetWindowLongPtr(Window::GetWindow().GetHandle(), GWL_STYLE, 
			WS_SYSMENU | WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE);
			MoveWindow(Window::GetWindow().GetHandle(), 0, 0, 1280, 800, TRUE);

			DEVMODE dmScreenSettings;								// Device Mode
			memset(&dmScreenSettings,0,sizeof(dmScreenSettings));	// Makes Sure Memory's Cleared

			dmScreenSettings.dmSize=sizeof(dmScreenSettings);		// Size Of The Devmode Structure
			dmScreenSettings.dmPelsWidth	= 1280;				// Selected Screen Width
			dmScreenSettings.dmPelsHeight	= 800;				// Selected Screen Height
			dmScreenSettings.dmBitsPerPel	= 32;					// Selected Bits Per Pixel
			dmScreenSettings.dmDisplayFrequency = 60;
			dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT|DM_DISPLAYFREQUENCY;

			ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL;
		}
		else
		{
			RECT rect;
			rect.left = 0;
			rect.top = 0;
			rect.right = 1280;
			rect.bottom = 800;

			ChangeDisplaySettings(0, 0) == DISP_CHANGE_SUCCESSFUL;

			SetWindowLongPtr(Window::GetWindow().GetHandle(), GWL_STYLE, WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE);
			AdjustWindowRect(&rect, WS_CAPTION | WS_POPUPWINDOW, FALSE);
			MoveWindow(Window::GetWindow().GetHandle(), 0, 0, rect.right-rect.left, rect.bottom-rect.top, TRUE);
		}

	}

};